package com.example.myapplication.provider;

import com.example.myapplication.ResourceTable;
import com.example.myapplication.beans.IndexImg;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class IndexImgCarousel extends PageSliderProvider {
    //只给资源
    //private List<Integer> imageIds;
    //只给对象
    private List<IndexImg> list;
    private AbilitySlice abilitySlice;

    public IndexImgCarousel(List<IndexImg> list, AbilitySlice abilitySlice) {
        this.list = list;
        this.abilitySlice = abilitySlice;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        //先拿数据
        IndexImg indexImg = list.get(i);

        //再拿模板
        DirectionalLayout template = (DirectionalLayout) LayoutScatter.getInstance(abilitySlice).parse(ResourceTable.Layout_index_image_template,null,false);

        //再把数据渲染到模板
        Image image = template.findComponentById(ResourceTable.Id_index_image);
        image.setPixelMap(indexImg.getIndexResourceId());

        //把渲染后的模板加到布局中
        componentContainer.addComponent(template);
        return template;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return true;
    }

}
